/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucm;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import model.ResultView_Model;
import model.Stu_Info_Model;
import view.Result_View;

/**
 *
 * @author 马凯
 */
public class ShowResult_UC {

    private static ShowResult_UC sruc;
    private String courseID;
    private Result_View rv;
    private ResultView_Model rvm;
    private JTable result_Ta;
    private TableModel tm;
    private Stu_Info_Model sim;
    private List stuName;
    private List stuID;
    private List stuCID;
    private List stuAttend;

    public static ShowResult_UC getInstance(String courseID) {
        if (sruc == null) {
            sruc = new ShowResult_UC();
            sruc.courseID = courseID;
        }
        return sruc;
    }

    public void run() {
        rv = new Result_View();
        rv.setVisible(true);
        rvm = rv.getViewModel();
        this.result_Ta = rvm.getResult_Ta();
        this.tm = result_Ta.getModel();

        try {
            System.out.println("The path is " + this.courseID + ".xml");
            XMLDecoder decoder =
                    new XMLDecoder(
                    new BufferedInputStream(new FileInputStream(this.courseID + ".xml")));
            
            sim = (Stu_Info_Model) decoder.readObject();
            this.stuCID = sim.getCid();
            this.stuID = sim.getStu_ID();
            this.stuName = sim.getName();
            this.stuAttend = sim.getAttend_Num();

            for (int i = 0; i < stuCID.size(); i++) {
                tm.setValueAt(stuName.get(i), i, 0);
                tm.setValueAt(stuID.get(i), i, 1);
                tm.setValueAt(stuAttend.get(i), i, 2);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
